package yxy.game.pm2.module.resonate;

import org.apache.commons.lang3.StringUtils;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.function;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.message.ResonateMessage;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.conquest.ConquestModule;

import java.util.*;

//共鸣系统
public class ResonateHandle {

    /**
     * 获取最低级的那个武将
     *
     * @param module
     * @return
     */
    public static HashMap<String, ResonateHeroMsg> getMinimumLevel(ResonateModule module) {

        LinkedHashMap<String, ResonateHeroMsg> map = new LinkedHashMap<>();//如果场上的武将不满足6个则返回空map

        HashMap<String, ResonateHeroMsg> aboveHeros = module.getAboveHeros();

        ArrayList<ResonateHeroMsg> list = new ArrayList<>();

        Set<Map.Entry<String, ResonateHeroMsg>> entries = aboveHeros.entrySet();
        for (Map.Entry<String, ResonateHeroMsg> entry : entries) {
            if (StringUtils.isEmpty(entry.getValue().getCode())) continue;
            list.add(entry.getValue());
        }

        if (list.size() != 6) {
            return map;
        }

        Collections.sort(list, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                ResonateHeroMsg msg1 = (ResonateHeroMsg) o1;
                ResonateHeroMsg msg2 = (ResonateHeroMsg) o2;
                return msg1.getLevel() - msg2.getLevel();
            }
        });

        for (int i = 0; i < list.size(); i++) {
            map.put(list.get(i).getCode(),list.get(i));
        }

        return map;
    }

    /**
     * 升级/升星/升阶/重生/玉玺解锁/献祭/七星祭坛/置换神殿/高级祭坛/武将置换  改变共鸣的武将自适应处理
     * 涉及到共鸣的武将信息修改
     *
     * @param hero
     */
    public static Result heroIsResonateHero(Hero hero, String playerCode) {
        ConquestModule conquestModule = ConquestModule.get(playerCode);
        function data = DataCenter.getData(10046, function.class);
        if (conquestModule.getMapId() <= data.getMapnum()) {
            //没开启，不走逻辑
            return Result.Success;
        }

        ResonateModule module = ResonateModule.get(playerCode);
        ArrayList<String> resonateHeroCodeList = getResonateHeroCodeList(module);

        if (resonateHeroCodeList.contains(hero.getCode())) {
            return Result.ResonateConfirmIsResonateHero;
        }
        return Result.Success;
    }

    /**
     * 升级/升星/升阶/重生/玉玺解锁/献祭/七星祭坛/置换神殿/高级祭坛/武将置换  改变共鸣的武将自适应处理
     * 涉及到共鸣的武将信息修改
     *
     * @param heros
     */
    public static Result heroIsResonateHero(List<String> heros, String playerCode) {
        ConquestModule conquestModule = ConquestModule.get(playerCode);
        function data = DataCenter.getData(10046, function.class);
        if (conquestModule.getMapId() <= data.getMapnum()) {
            //没开启，不走逻辑
            return Result.Success;
        }

        ResonateModule module = ResonateModule.get(playerCode);
        ArrayList<String> resonateHeroCodeList = getResonateHeroCodeList(module);

        for (int i = 0; i < heros.size(); i++) {
            if (resonateHeroCodeList.contains(heros.get(i))) {
                return Result.ResonateConfirmIsResonateHero;
            }
        }
        return Result.Success;
    }

    /**
     * 升级/  改变共鸣场上的武将自适应处理
     * 武将提升操作
     *
     * @param hero
     * @param playerCode
     * @return
     */
    public static void heroIsAboveHeroAndPromote(Hero hero, String playerCode) {
        try {
            ConquestModule conquestModule = ConquestModule.get(playerCode);
            function data = DataCenter.getData(10046, function.class);
            if (conquestModule.getMapId() <= data.getMapnum()) {
                //没开启，不走逻辑
                return;
            }

            ResonateModule module = ResonateModule.get(playerCode);
            ArrayList<String> aboveHeroCodeList = getAboveHeroCodeList(module);

            if (!aboveHeroCodeList.contains(hero.getCode())) return;

            HashMap<String, ResonateHeroMsg> aboveHeros = module.getAboveHeros();

            for (ResonateHeroMsg value : aboveHeros.values()) {
                if (hero.getCode().equals(value.getCode())) {
                    value.setLevel(hero.getLevel());
                }
            }

            module.set();

            HashMap<String, ResonateHeroMsg> minimumLevelMap = ResonateHandle.getMinimumLevel(module);

            //是最低级的那个武将才进行共鸣系统变更
            if (minimumLevelMap.size() > 0 && minimumLevelMap.containsKey(hero.getCode())) {

                ResonateHeroMsg minimumHeroMsg = null;
                for (ResonateHeroMsg value : minimumLevelMap.values()) {
                    minimumHeroMsg = value;//最低级的那个武将
                    break;
                }

                HashMap<String, Integer> map = BattleHandle.addStarAndStage(hero.getDataId(), 0, 0, minimumHeroMsg.getLevel());//计算阶级

                changeHero(module, minimumHeroMsg, map, playerCode);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * /重生/高级祭坛/  改变共鸣场上的武将自适应处理
     * 武将减弱操作
     *
     * @param hero
     * @param playerCode
     * @return
     */
    public static void heroIsAboveHeroAndWeaken(Hero hero, String playerCode) {
        try {
            ConquestModule conquestModule = ConquestModule.get(playerCode);
            function data = DataCenter.getData(10046, function.class);
            if (conquestModule.getMapId() <= data.getMapnum()) {
                //没开启，不走逻辑
                return;
            }

            ResonateModule module = ResonateModule.get(playerCode);
            ArrayList<String> aboveHeroCodeList = getAboveHeroCodeList(module);

            if (!aboveHeroCodeList.contains(hero.getCode())) return;

            HashMap<String, ResonateHeroMsg> minimumLevelMap = ResonateHandle.getMinimumLevel(module);

            for (ResonateHeroMsg value : module.getAboveHeros().values()) {
                if (hero.getCode().equals(value.getCode())) {
                    value.setLevel(hero.getLevel());
                }
            }
            module.set();

            //跟最低的那个等级比较，如果还要更低，就更新共鸣
            Boolean isGone = false;
            ResonateHeroMsg minimumHeroMsg = null;
            for (ResonateHeroMsg value : minimumLevelMap.values()) {
                if (hero.getLevel() < value.getLevel()) {
                    isGone = true;
                }
            }

            if (isGone) {
                for (ResonateHeroMsg value : module.getAboveHeros().values()) {
                    if (hero.getCode().equals(value.getCode())) {
                        minimumHeroMsg = value;
                    }
                }
            }

            if (isGone) {
                HashMap<String, Integer> map = BattleHandle.addStarAndStage(hero.getDataId(), 0, 0, minimumHeroMsg.getLevel());//计算阶级
                changeHero(module, minimumHeroMsg, map, playerCode);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 升星/玉玺解锁/主公府置换/七星祭坛/置换神殿/武将献祭 改变共鸣场上的武将自适应处理
     * 武将分解操作
     *
     * @param heroCodes
     * @param playerCode
     * @return
     */
    public static void heroIsAboveHeroAndResolve(List<String> heroCodes, String playerCode) {
        try {
            ConquestModule conquestModule = ConquestModule.get(playerCode);
            function data = DataCenter.getData(10046, function.class);
            if (conquestModule.getMapId() <= data.getMapnum()) {
                //没开启，不走逻辑
                return;
            }

            ResonateModule module = ResonateModule.get(playerCode);
            ArrayList<String> aboveHeroCodeList = getAboveHeroCodeList(module);

            for (int i = 0; i < heroCodes.size(); i++) {
                String code = heroCodes.get(i);

                if (!aboveHeroCodeList.contains(code)) continue;

                String position = null;
                for (ResonateHeroMsg value : module.getAboveHeros().values()) {
                    if (code.equals(value.getCode())) {
                        position = value.getPosition();
                    }
                }

                if (aboveHeroCodeList.size() == 6) {
                    //共鸣武将恢复原样
                    ResonateHandle resonateHandle = new ResonateHandle();
                    BattleHandle battleHandle = new BattleHandle();
                    resonateHandle.unOpenUpdate(module, playerCode);
                    battleHandle.powerUpdate(playerCode);
                }


                module.getAboveHeros().remove(position);
                module.setOpen(false);
                module.set();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 升星/玉玺解锁/主公府置换/七星祭坛/置换神殿/武将献祭改变共鸣场上的武将自适应处理
     * 武将分解操作
     *
     * @param hero
     * @param playerCode
     * @return
     */
    public static void heroIsAboveHeroAndResolve(Hero hero, String playerCode) {
        try {
            ConquestModule conquestModule = ConquestModule.get(playerCode);
            function data = DataCenter.getData(10046, function.class);
            if (conquestModule.getMapId() <= data.getMapnum()) {
                //没开启，不走逻辑
                return;
            }

            ResonateModule module = ResonateModule.get(playerCode);
            ArrayList<String> aboveHeroCodeList = getAboveHeroCodeList(module);

            if (!aboveHeroCodeList.contains(hero.getCode())) return;

            String position = null;
            for (ResonateHeroMsg value : module.getAboveHeros().values()) {
                if (hero.getCode().equals(value.getCode())) {
                    position = value.getPosition();
                }
            }

            if (aboveHeroCodeList.size() == 6) {
                //共鸣武将恢复原样
                ResonateHandle resonateHandle = new ResonateHandle();
                BattleHandle battleHandle = new BattleHandle();
                resonateHandle.unOpenUpdate(module, playerCode);
                battleHandle.powerUpdate(playerCode);
            }

            module.getAboveHeros().remove(position);
            module.setOpen(false);
            module.set();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 更新撤退或不激活的共鸣武将状态
     *
     * @param module
     */
    public void unOpenUpdate(ResonateModule module, String playerCode) {
        ArrayList<ResonateHeroMsg> resonateHeros = module.getResonateHeros();
        for (int i = 0; i < resonateHeros.size(); i++) {
            ResonateHeroMsg msg = resonateHeros.get(i);
            //状态还原
            String code = msg.getCode();
            if (StringUtils.isEmpty(code)) continue;
            msg.setStage(msg.getOriginalStage());
            msg.setLevel(msg.getOriginalLevel());
            Hero hero = Hero.get(playerCode, code);
            hero.setStage(msg.getOriginalStage());
            hero.setLevel(msg.getOriginalLevel());
            Hero.set(playerCode, hero);
        }
    }

    /**
     * 场上位置的武将返回
     *
     * @param module
     */
    public static ArrayList<ResonateMessage.HeroMsg> getAboveHeroList(ResonateModule module) {
        HashMap<String, ResonateHeroMsg> aboveHeros = module.getAboveHeros();

        ArrayList<ResonateMessage.HeroMsg> aboveHeroList = new ArrayList<>();//阵上的武将

        for (ResonateHeroMsg value : aboveHeros.values()) {
            ResonateMessage.HeroMsg.Builder builder = ResonateMessage.HeroMsg.newBuilder();
            builder.setStage(value.getStage());
            builder.setStar(value.getStar());
            builder.setLevel(value.getLevel());
            builder.setPosition(value.getPosition());
            builder.setHeroId(value.getHeroId());
            builder.setCode(value.getCode());
            aboveHeroList.add(builder.build());
        }
        return aboveHeroList;
    }

    /**
     * 共鸣的武将返回
     *
     * @param module
     */
    public static ArrayList<ResonateMessage.HeroMsg> getResonateHeros(ResonateModule module) {
        ArrayList<ResonateHeroMsg> resonateHeros = module.getResonateHeros();

        ArrayList<ResonateMessage.HeroMsg> resonateHeroList = new ArrayList<>();//共鸣的武将

        for (int i = 0; i < resonateHeros.size(); i++) {
            ResonateHeroMsg resonateHeroMsg = resonateHeros.get(i);
            ResonateMessage.HeroMsg.Builder builder = ResonateMessage.HeroMsg.newBuilder();
            builder.setStage(resonateHeroMsg.getStage());
            builder.setStar(resonateHeroMsg.getStar());
            builder.setLevel(resonateHeroMsg.getLevel());
            builder.setHeroId(resonateHeroMsg.getHeroId());
            String code = resonateHeroMsg.getCode() == null ? "" : resonateHeroMsg.getCode();
            builder.setCode(code);
            resonateHeroList.add(builder.build());
        }
        return resonateHeroList;
    }

    /**
     * 场上位置的武将返回 code
     *
     * @param module
     */
    public static ArrayList<String> getAboveHeroCodeList(ResonateModule module) {

        HashMap<String, ResonateHeroMsg> aboveHeros = module.getAboveHeros();

        ArrayList<String> aboveHeroList = new ArrayList<>();//阵上的武将

        for (ResonateHeroMsg value : aboveHeros.values()) {

            aboveHeroList.add(value.getCode());
        }
        return aboveHeroList;
    }

    /**
     * 共鸣位置的武将返回 code
     *
     * @param module
     */
    public static ArrayList<String> getResonateHeroCodeList(ResonateModule module) {

        ArrayList<ResonateHeroMsg> resonateHeros = module.getResonateHeros();

        ArrayList<String> aboveHeroList = new ArrayList<>();//阵上的武将

        for (int i = 0; i < resonateHeros.size(); i++) {
            ResonateHeroMsg heroMsg = resonateHeros.get(i);
            if (StringUtils.isEmpty(heroMsg.getCode())) continue;
            aboveHeroList.add(resonateHeros.get(i).getCode());
        }

        return aboveHeroList;
    }

    /**
     * 共鸣激活逻辑
     *
     * @param hero
     * @param module
     * @param playerCode
     */
    public static ResonateHeroMsg ResonateLogic(Hero hero, ResonateModule module, String playerCode) {
        HashMap<String, ResonateHeroMsg> minimumLevelMap = ResonateHandle.getMinimumLevel(module);
        ResonateHeroMsg minimumHeroMsg = null;
        for (ResonateHeroMsg value : minimumLevelMap.values()) {
            minimumHeroMsg = value;//最低级的那个武将
            break;
        }
        HashMap<String, Integer> map = BattleHandle.addStarAndStage(hero.getDataId(), 0, 0, minimumHeroMsg.getLevel());//计算阶级

        //封装返回
        ResonateHeroMsg heroMsg = new ResonateHeroMsg();
        heroMsg.setCode(hero.getCode());
        heroMsg.setHeroId(hero.getDataId());
        heroMsg.setLevel(minimumHeroMsg.getLevel());
        heroMsg.setStar(hero.getStar());
        heroMsg.setStage(map.get("stage"));
        heroMsg.setOriginalLevel(hero.getLevel());
        heroMsg.setOriginalStage(hero.getStage());

        Hero newHero = Hero.get(playerCode, hero.getCode());
        newHero.setStage(map.get("stage"));
        newHero.setLevel(minimumHeroMsg.getLevel());
        Hero.set(playerCode, newHero);

        return heroMsg;
    }


    /**
     * 改变共鸣武将缓存
     *
     * @param module
     * @param minimumHeroMsg
     * @param map
     * @param playerCode
     */
    public static void changeHero(ResonateModule module, ResonateHeroMsg minimumHeroMsg, HashMap<String, Integer> map, String playerCode) {

        for (ResonateHeroMsg msg : module.getResonateHeros()) {
            if (StringUtils.isEmpty(msg.getCode())) continue;
            msg.setCode(msg.getCode());
            msg.setHeroId(msg.getHeroId());
            msg.setLevel(minimumHeroMsg.getLevel());
            msg.setStar(msg.getStar());
            msg.setStage(map.get("stage"));

            Hero msgHero = Hero.get(playerCode, msg.getCode());
            msgHero.setStage(map.get("stage"));
            msgHero.setLevel(minimumHeroMsg.getLevel());
            Hero.set(playerCode, msgHero);
        }

        //更新战力
        BattleHandle battleHandle = new BattleHandle();
        battleHandle.powerUpdate(playerCode);

        module.set();
    }


}
